$(document).ready(function () {
});

/*显示文收藏夹*/
function showFolder(that) {
    document.getElementById("folderName").innerText = $(that).attr("data-favoriteName");
    document.getElementById("contentNum").innerText = $(that).attr("data-contentNum") + "个内容";
    document.getElementById("folderInfo").style.display = 'block';
    getContent(that);
}

/*新建收藏夹*/
function addFolder() {
    document.getElementById("newFolder").style.display = "block";
}

/*向后端发起新建文件夹请求*/
function insertFolder() {
    let folderName = document.getElementById("newFolderInput").value;
    $.ajax({
        url: 'insertFolder',
        method: "get",
        contentType: 'application/json;charset=UTF-8',
        data: {'folderName': folderName},
        success: function (data) {
            if (data === 'ERROR') {
                document.getElementById("newFolderError").style.display = "block";
            } else {
                document.getElementById("newFolder").style.display = "none";
                location.reload();
            }
        },
        error: function () {
            document.getElementById("newFolderError").style.display = "block";
        }
    })
}

/*获取收藏内容*/
function getContent(that) {
    $.ajax({
        url: 'getFolderContent',
        method: 'GET',
        contentType: 'application/json;charset=UTF-8',
        data: {'objectId': $(that).attr('data-objectId')},
        success: function (data) {
            console.log(data.data.length);
            let div = document.getElementById('rightCollection');
            $('#rightCollection').find("div[class='folderContent']").remove();

            for (let i = 0; i < data.data.length; i++) {
                let mydiv = document.createElement('div');
                mydiv.className = 'folderContent';

                let myTitle = document.createElement('a');
                myTitle.href='/yuanwen/getViewBrowseAritcle?objectId='+data.data[i].objectid;
                myTitle.innerText = data.data[i].title;
                myTitle.className = 'contentTitle';

                let myCreate = document.createElement('p');
                myCreate.innerText = data.data[i].createtime;
                myCreate.className = 'contentCreate';

                mydiv.append(myTitle);
                mydiv.append(myCreate);
                div.append(mydiv);
            }
        },
        error: function () {
            $('#rightCollection').find("div[class='folderContent']").remove();
        }
    })
}
